<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const inputQuestion = ref('')

// GIF 动画状态
const animations = ref({
  intro: '/intro.gif',  // 介绍动画，会在页面加载后显示
  loop: '/loop.gif'    // 循环动画，会在介绍动画播放后显示
})
const currentAnimation = ref('')
const isLoading = ref(true)  // 添加加载状态

// 添加资源加载状态
const assetsLoaded = ref(true)  // 默认为true，因为使用静态文件

// 切换动画的方法
const switchAnimation = () => {
  // 初始显示intro动画
  currentAnimation.value = animations.value.intro
  
  // 3秒后切换到循环动画
  setTimeout(() => {
    currentAnimation.value = animations.value.loop
  }, 3000)
}

// 处理发送按钮点击
const handleSend = () => {
  const question = inputQuestion.value.trim()
  if (question) {
    goToChat(question)
    inputQuestion.value = ''
  }
}

// 跳转到对话页面
const goToChat = (question?: string) => {
  router.push({
    path: '/chat',
    query: question ? { q: question } : undefined
  })
}

onMounted(() => {
  // 页面加载后切换动画
  switchAnimation()
  isLoading.value = false
})
</script>

<template>
  <div class="guest-home">
    <div class="ai-avatar-section">
      <div class="animation-container">
        <img class="avatar-animation" :src="currentAnimation" alt="AI Assistant" />
      </div>
    </div>
    <div class="content-section">
      <div class="ai-intro">
        <h2>Hi, 我是智能助手</h2>
        <p>为您提供各类问题的详细解答</p>
      </div>

      <!-- 快捷问题区域 -->
      <div class="quick-questions">
        <div class="question-grid">
          <div class="question-item" @click="goToChat('如何提升党员党建意识？')">
            <span>如何提升党员党建意识？</span>
          </div>
          <div class="question-item" @click="goToChat('党建与业务怎样融合？')">
            <span>党建与业务怎样融合？</span>
          </div>
          <div class="question-item" @click="goToChat('组织生活如何创新？')">
            <span>组织生活如何创新？</span>
          </div>
          <div class="question-item" @click="goToChat('党建考核机制咋优化？')">
            <span>党建考核机制咋优化？</span>
          </div>
        </div>
      </div>

      <!-- 输入框区域 -->
      <div class="input-section">
        <div class="input-wrapper">
          <input type="text" v-model="inputQuestion" placeholder="请输入问题..." @keyup.enter="handleSend" />
          <div class="send-btn" @click="handleSend">
            发送
          </div>
        </div>
      </div>
    </div>
  </div>
</template>


<style scoped>
.guest-home {
  height: 100vh;
  background: linear-gradient(180deg, #eef6ff 0%, #f5f7fa 100%);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding-bottom: 20px;
}

.ai-avatar-section {
  height: 70vh;
  position: relative;
  overflow: hidden;
}

.animation-container {
  width: 100%;
  height: 100%;
  position: relative;
}

.avatar-animation {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: auto;
  min-height: 100%;
  object-fit: cover;
  object-position: top;
}

.loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.8);
}

.content-section {
  min-height: 30vh;
  position: relative;
  display: flex;
  flex-direction: column;
  background: linear-gradient(180deg, rgba(238, 246, 255, 0.9) 0%, #f5f7fa 100%);
  backdrop-filter: blur(10px);
}

.ai-intro {
  text-align: center;
  color: #333;
  padding: 0px 10px 8px;
}

.ai-intro h2 {
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 2px;
}

.ai-intro p {
  font-size: 13px;
  color: #666;
  margin: 0;
}

.quick-questions {
  padding: 0 16px;
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  margin-bottom: 14px;
}

.question-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 5px;
}

.question-item {
  background: white;
  padding: 4px;
  border-radius: 12px;
  text-align: center;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  cursor: pointer;
  transition: all 0.2s ease;
  border: 1px solid rgba(0, 0, 0, 0.08);
}

.question-item:active {
  transform: scale(0.98);
  background: #f5f7fa;
}

.question-item span {
  font-size: 13px;
  color: #928e8e;
  font-weight: 500;
}

.input-section {
  padding: 0 15px 0px 15px;
  margin-top: auto;
}

.input-wrapper {
  background: white;
  border-radius: 24px;
  padding: 6px 6px 6px 16px;
  display: flex;
  align-items: center;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(0, 0, 0, 0.08);
  margin-bottom: 20px;
}

.input-wrapper input {
  flex: 1;
  height: 40px;
  border: none;
  outline: none;
  font-size: 15px;
  color: #333;
  background: transparent;
  padding: 0;
}

.input-wrapper input::placeholder {
  color: #999;
}

.send-btn {
  height: 40px;
  width: 100px;
  color: white;
  border-radius: 15px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 12px;
  font-weight: 500;
  transition: all 0.2s ease;
  background: #73899681;
}

.send-btn:active {
  transform: scale(0.98);
  background: var(--el-color-primary-dark-2);
}

.send-btn i {
  font-size: 12px;
}

.content-section::before {
  content: '';
  position: absolute;
  top: -10px;
  left: 0;
  right: 0;
  height: 10px;
  background: linear-gradient(180deg, rgba(0, 0, 0, 0.04) 0%, rgba(0, 0, 0, 0) 100%);
}

.loading-screen {
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: #f5f7fa;
}

.loading-screen .loading-icon {
  font-size: 40px;
  color: var(--el-color-primary);
  animation: rotate 1s linear infinite;
}

.loading-screen span {
  font-size: 14px;
  color: #666;
}
</style>